package com.pmenergy.control;

// Generated 15/08/2014 01:13:31 AM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pmenergy.model.FormaPago;

/**
 * Home object for domain model class FormaPago.
 * @see com.pmenergy.model.FormaPago
 * @author Hibernate Tools
 */
@Stateless
public class FormaPagoHome {

	private static final Log log = LogFactory.getLog(FormaPagoHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public int persist(FormaPago transientInstance) {
		int result=1;
		log.debug("persisting FormaPago instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
			return result;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			return -1;
		}
	}

	public void remove(FormaPago persistentInstance) {
		log.debug("removing FormaPago instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public FormaPago merge(FormaPago detachedInstance) {
		log.debug("merging FormaPago instance");
		try {
			FormaPago result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public FormaPago findById(BigDecimal id) {
		log.debug("getting FormaPago instance with id: " + id);
		try {
			FormaPago instance = entityManager.find(FormaPago.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<FormaPago> findAllFormasDePago() {
		log.debug("getting all Formas de Pago instances " );
		try {
			
			List<FormaPago> list = entityManager.createQuery("SELECT c FROM FormaPago c ").getResultList();
			log.debug("get successful");
			return list;
		} 
		/*catch (NoResultException re){
			return null;
		}*/
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<FormaPago> findFormaPagoByFields(String tipo, String descripcion) {
		log.debug("getting all Formas de Pago instances " );
		try {
			Query query;
			String q="SELECT c FROM FormaPago c  WHERE "					
					+  "c.descripcion LIKE :descripcion";
					
			if (tipo != null && !tipo.equals("")) q+=  " AND c.tipo = :tipo";
			
			query = entityManager.createQuery(q);	
			
			query.setParameter("descripcion","%" + descripcion + "%");
			
			if (tipo != null && !tipo.equals("")) query.setParameter("tipo",tipo.charAt(0));
			
			
			List<FormaPago> list = (List<FormaPago>)query.getResultList();
			log.debug("get successful");
			
			return list;
		} 
		/*catch (NoResultException re){
			return null;
		}*/
		catch (RuntimeException re) {
			log.error("get failed", re);
			return null;
			//throw re;
			
		}
	}
	
	public int updateFormaPago(BigDecimal formaPagoId,char tipo,String descripcion) {
		
		int updatedFormaPago;
		log.debug("updating Forma de Pago instance " );
		try {
			
			Query query;
			query = entityManager.createQuery("UPDATE FormaPago c SET c.tipo = :tipo,"
																		+  "c.descripcion = :descripcion"
																		+ " WHERE c.formaPagoId = :id");
			
																				
			query.setParameter("id",formaPagoId);
			query.setParameter("tipo",tipo);
			query.setParameter("descripcion",descripcion);
			
			updatedFormaPago = query.executeUpdate();
			return updatedFormaPago;
		}			
		catch (RuntimeException re) {
			log.error("get failed", re);				
			return -1;
			
		}
	}

	public int removeById(BigDecimal formaPagoId) {
		log.debug("removing Forma de Pago instance with id: " + formaPagoId);
		try {
			int deletedFormaPago = entityManager.createQuery("DELETE FROM FormaPago c WHERE c.formaPagoId = :id")
					.setParameter("id",formaPagoId).executeUpdate();
			log.debug("get successful");
			return deletedFormaPago;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
}
